package com.dq.Week1.day04.arr;

public class ArrUtils {
    public static void printIntArr(int[] arr) {
        //打印数组内容
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]);
        }
        System.out.println();
    }

    public static void printName(String[] nameArr) {

        for (int i = 0; i < nameArr.length; i++) {
            System.out.println(nameArr[i]);

        }

    }

    public static int maxArr(int[] arr) {
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (max < arr[i]) {
                max = arr[i];

            }
        }
        return max;
    }

    public static int minArr(int[] arr) {
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (min > arr[i]) {
                min = arr[i];

            }
        }
        return min;
    }

    public static double avgArr(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum / arr.length;
    }

    public static int indexStringArr(String[] arr,String target)
    {
        for (int i = 0; i < arr.length; i++) {
                if(target.equals(arr[i]))
                {
                    return i;
                }
                //没有找到返回-1

        }
        return -1;
    }

    public static int[] copyIntArr(int[] arr1,int[] arr2)
    {
        for (int i = 0; i < arr1.length; i++) {
            if(i == arr2.length)
            {
                break;
            }
            arr2[i] = arr1[i];
        }

        return arr2;
    }

    public static void printTargetArr(int[] oldArr,int oldStart,int[] newArr,int newStart,int length)
    {

        for (int i = oldStart; i < oldStart+length; i++) {
                newArr[newStart] = oldArr[i];
                        newStart++;
        }
    }


    public static void bubbleSort(int[] arr){
        for(int i=0;i<arr.length-1;i++)
        {
            for (int j = 0; j<arr.length-1-i; j++) {
                if(arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                }
            }
        }


    }
    public static void selectionSort(int[] arr){
        for (int i = 0; i < arr.length - 1; i++) {
            int minIndex = i;
            for (int j = 0; j < arr.length; j++) {
                if(arr[j]<arr[minIndex])
                {
                    minIndex = j;
                }
            }
        }
    }
    public static void swap(int[] arr,int index1,int index2)
    {
        int temp = arr[index1];
        arr[index1] = arr[index2];
        arr[index2] = temp;
    }
}
